﻿package me.idashu.attg.webService;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.idashu.attg.domain.AttenEntity;
import me.idashu.attg.domain.AuditeEntity;
import me.idashu.attg.domain.CcollectEntity;
import me.idashu.attg.domain.ContextEntity;
import me.idashu.attg.domain.ContributionEntity;
import me.idashu.attg.domain.FeedBackEntity;
import me.idashu.attg.domain.MessageEntity;
import me.idashu.attg.domain.NoteEntity;
import me.idashu.attg.domain.PageEntity;
import me.idashu.attg.domain.PersonInfoEntity;
import me.idashu.attg.domain.RecommendEntity;
import me.idashu.attg.domain.ScanEntity;
import me.idashu.attg.domain.UserEntity;
import me.idashu.attg.domain.UserrecommEntity;
import me.idashu.attg.service.AttenService;
import me.idashu.attg.service.AuditeService;
import me.idashu.attg.service.CcollectService;
import me.idashu.attg.service.ContextService;
import me.idashu.attg.service.ContributionService;
import me.idashu.attg.service.FeedbackService;
import me.idashu.attg.service.MessageService;
import me.idashu.attg.service.NoteService;
import me.idashu.attg.service.PersonInfoService;
import me.idashu.attg.service.RecommendService;
import me.idashu.attg.service.ScanService;
import me.idashu.attg.service.UserService;
import me.idashu.attg.service.UserrecommService;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AllWebSerivce {

	@Autowired
	private ContextService contextService;

	@Autowired
	private ContributionService contributionService;

	@Autowired
	private RecommendService recommendService;

	@Autowired
	private AuditeService auditeService;

	@Autowired
	private AttenService attenService;

	@Autowired
	private PersonInfoService personInfoService;

	@Autowired
	private FeedbackService feedbackService;

	@Autowired
	private UserService userService;

	@Autowired
	private CcollectService ccollectService;

	@Autowired
	private NoteService noteService;

	@Autowired
	private ScanService scanService;

	@Autowired
	private MessageService messageService;

	@Autowired
	private UserrecommService userrecommService;

	@RequestMapping(value = "/findAllContexts", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject findAllContexts(
			@ModelAttribute ContextEntity contextEntity) {
		JSONObject json = new JSONObject();
		try {
			PageEntity pageEntity = new PageEntity();
			pageEntity.setPage(0);
			pageEntity.setTotalPage(100);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageEntity", pageEntity);
			map.put("contextEntity", contextEntity);
			List<ContextEntity> contexts = contextService.findAllContexts(map);
			json.put("status", 200);
			json.put("contexts", contexts);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());

		}
		return json;

	}

	@RequestMapping(value = "/findOneContext", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> findOneContext(
			@ModelAttribute ContextEntity contextEntity) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			ContextEntity context = contextService
					.findContextById(contextEntity.getId());
			ContributionEntity contribution = new ContributionEntity();
			contribution.setContext_id(contextEntity.getId());
			contribution.setFlag("Y");
			List<ContributionEntity> contributions = contributionService
					.findContributionsByContextIdAndFlag(contribution);
			map.put("status", 200);
			map.put("context", context);
			map.put("contributions", contributions);
		} catch (Exception ex) {
			ex.printStackTrace();
			map.put("status", 400);
			map.put("message", ex.getMessage());
		}
		return map;

	}

	@RequestMapping(value = "/insertContribution", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertContribution(
			@ModelAttribute ContributionEntity contributionEntity) {
		JSONObject json = new JSONObject();
		try {
			contributionEntity.setTime(new Date());
			contributionEntity.setFlag("N");
			contributionService.insertContribution(contributionEntity);
			json.put("status", 200);
			json.put("message", "投稿成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/insertRecommend", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertRecommend(@ModelAttribute RecommendEntity recommend) {
		JSONObject json = new JSONObject();
		try {
			recommend.setTime(new Date());
			recommend.setFlag("N");
			recommendService.insertRecommend(recommend);
			json.put("status", 200);
			json.put("message", "添加成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;

	}

	@RequestMapping(value = "/insertAudite", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertAudite(@ModelAttribute AuditeEntity auditeEntity) {
		JSONObject json = new JSONObject();
		try {
			auditeEntity.setTime(new Date());
			auditeService.insertAudite(auditeEntity);
			json.put("status", 200);
			json.put("message", "添加审核成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/findReCommends", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject findReCommends(
			@ModelAttribute RecommendEntity recommendEntity,
			@ModelAttribute PageEntity pageEntity) {
		JSONObject json = new JSONObject();
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			int count = recommendService.findCountRecommends(recommendEntity);
			pageEntity.setTotalPage(count);
			pageEntity.setPageLess(pageEntity.getPage());
			param.put("pageEntity", pageEntity);
			param.put("recommendEntity", recommendEntity);
			List<RecommendEntity> recommends = recommendService
					.findRecommends(param);
			json.put("status", 200);
			json.put("recommends", recommends);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/updateReCommendRescore", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject updateReCommendRescore(
			@ModelAttribute RecommendEntity recommend,
			@ModelAttribute UserrecommEntity userrecommEntity) {
		JSONObject json = new JSONObject();
		try {
			userrecommEntity.setTime(new Date());
			userrecommEntity.setId(recommend.getId());
			recommendService.updateRescoreById(recommend.getId());
			userrecommService.insertUserrecomm(userrecommEntity);
			json.put("status", 200);
			json.put("message", "推荐成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/insertAtten", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertAtten(@ModelAttribute AttenEntity attenEntity) {
		JSONObject json = new JSONObject();
		try {
			attenEntity.setTime(new Date());
			attenService.insertAttenEntity(attenEntity);
			json.put("status", 200);
			json.put("message", "添加关注成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/deleteAtten", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject deleteAtten(@ModelAttribute AttenEntity attenEntity) {
		JSONObject json = new JSONObject();
		try {
			attenEntity.setTime(new Date());
			attenService.deleteAttenEntity(attenEntity);
			json.put("status", 200);
			json.put("message", "取消关注成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/findAttensOrAtteneds", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject findAttensOrAtteneds(
			@ModelAttribute AttenEntity attenEntity,
			@ModelAttribute PageEntity pageEntity) {
		JSONObject json = new JSONObject();
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			int count = attenService.findCountAttensByUserId(attenEntity);
			pageEntity.setTotalPage(count);
			pageEntity.setPageLess(pageEntity.getPage());
			param.put("pageEntity", pageEntity);
			param.put("attenEntity", attenEntity);
			List<UserEntity> users = attenService.findAttensByUserId(param);
			List<UserEntity> rusers = new ArrayList<UserEntity>();
			if (users != null && users.size() > 0) {
				for (UserEntity userEntity : users) {
					NoteEntity noteEntity = noteService
							.findNewNotesByUserId(userEntity.getId());
					userEntity.setNoteEntity(noteEntity);
					rusers.add(userEntity);
				}
			}
			json.put("status", 200);
			json.put("users", rusers);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/insertPersonInfo", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertPersonInfo(
			@ModelAttribute PersonInfoEntity personInfoEntity) {
		JSONObject json = new JSONObject();
		try {
			personInfoEntity.setTime(new Date());
			personInfoEntity.setStatues("E");
			personInfoEntity.setFlag("N");
			personInfoService.insertPersonInfo(personInfoEntity);
			json.put("status", 200);
			json.put("message", "发送私信成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/findOwnPersonInfos", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject findOwnNPersonInfos(
			@ModelAttribute PersonInfoEntity personInfoEntity) {
		JSONObject json = new JSONObject();
		try {
			List<PersonInfoEntity> personinfos = personInfoService
					.getPersoninfos(personInfoEntity);
			json.put("status", 200);
			json.put("personinfos", personinfos);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/updatePersonInfoToReaded", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject updatePersonInfoToReaded(@RequestParam("toid") long toId) {
		JSONObject json = new JSONObject();
		try {
			personInfoService.updateOwnPersonInfo(toId);
			json.put("status", 200);
			json.put("message", "修改成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/insertFeedBack", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertFeedBack(
			@ModelAttribute FeedBackEntity feedBackEntity) {
		JSONObject json = new JSONObject();
		try {
			feedbackService.insertFeedback(feedBackEntity);
			json.put("status", 200);
			json.put("message", "添加意见反馈成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	/**
	 * 登录
	 * 
	 * @return
	 */
	@RequestMapping(value = "/userlogin", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> login(@ModelAttribute UserEntity userEntity) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			UserEntity user = userService.getUser(userEntity);
			if (user == null) {
				map.put("status", 400);
				map.put("message", "用户名或者密码错误！");
			} else {
				map.put("status", 200);
				map.put("user", user);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			map.put("status", 400);
			map.put("message", ex.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "/getOneUser", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getOneUser(@ModelAttribute UserEntity userEntity) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			UserEntity user = userService.findOneUserById(userEntity.getId());
			map.put("status", 200);
			map.put("user", user);
		} catch (Exception ex) {
			ex.printStackTrace();
			map.put("status", 400);
			map.put("message", ex.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "/updateUserPassword", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateUserPassword(
			@ModelAttribute UserEntity userEntity) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			userService.updatePasswordById(userEntity);
			map.put("status", 200);
			map.put("message", "修改密码成功");
		} catch (Exception ex) {
			ex.printStackTrace();
			map.put("status", 400);
			map.put("message", ex.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "/findContextsSeq", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> findContextsSeq(
			@ModelAttribute ContextEntity contextEntity,
			@ModelAttribute PageEntity pageEntity) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			int count = contextService.findCountAllContexts(contextEntity);
			Map<String, Object> param = new HashMap<String, Object>();
			pageEntity.setTotalPage(count);
			pageEntity.setPageLess(pageEntity.getPage());
			param.put("pageEntity", pageEntity);
			param.put("seq", contextEntity.getSeq());
			List<ContextEntity> contexts = contextService.findContextsSeq(param);
			map.put("status", 200);
			map.put("contexts", contexts);
		} catch (Exception ex) {
			ex.printStackTrace();
			map.put("status", 400);
			map.put("message", ex.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "/insertCcollect", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertCcollect(
			@ModelAttribute CcollectEntity ccollectEntity) {
		JSONObject json = new JSONObject();
		try {
			ccollectEntity.setTime(new Date());
			ccollectService.insertCcollect(ccollectEntity);
			json.put("status", 200);
			json.put("message", "添加收藏成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/deleteCcollect", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject deleteCcollect(
			@ModelAttribute CcollectEntity ccollectEntity) {
		JSONObject json = new JSONObject();
		try {
			ccollectService.deleteCcollect(ccollectEntity);
			json.put("status", 200);
			json.put("message", "删除收藏成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/searchUsers", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject searchUsers(@ModelAttribute UserEntity userEntity,
			@ModelAttribute PageEntity pageEntity) {
		JSONObject json = new JSONObject();
		try {
			int count = userService.searchCountUsers(userEntity);
			Map<String, Object> param = new HashMap<String, Object>();
			pageEntity.setTotalPage(count);
			pageEntity.setPageLess(pageEntity.getPage());
			param.put("pageEntity", pageEntity);
			param.put("userEntity", userEntity);
			List<UserEntity> users = userService.searchUsers(param);
			json.put("status", 200);
			json.put("users", users);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/updateSetStatus", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject updateSetAtten(@ModelAttribute UserEntity userEntity) {
		JSONObject json = new JSONObject();
		try {
			userService.updateSetAttenById(userEntity);
			json.put("status", 200);
			json.put("message", "设置关注成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/insertScan", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertScan(@ModelAttribute ScanEntity scanEntity) {
		JSONObject json = new JSONObject();
		try {
			scanEntity.setTime(new Date());
			scanService.insertScan(scanEntity);
			json.put("status", 200);
			json.put("message", "设置足迹成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/findScansByUser", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject findScansByUser(@ModelAttribute ScanEntity scanEntity,
			@ModelAttribute PageEntity pageEntity) {
		JSONObject json = new JSONObject();
		try {
			int count = scanService.findCountScanByUser(scanEntity);
			Map<String, Object> param = new HashMap<String, Object>();
			pageEntity.setTotalPage(count);
			pageEntity.setPageLess(pageEntity.getPage());
			param.put("pageEntity", pageEntity);
			param.put("scanEntity", scanEntity);
			List<ScanEntity> scans = scanService.findScansByUser(param);
			json.put("status", 200);
			json.put("scans", scans);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/findMessagesByUser", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject findMessagesByUser(
			@ModelAttribute MessageEntity messageEntity,
			@ModelAttribute PageEntity pageEntity) {
		JSONObject json = new JSONObject();
		try {
			int count = messageService.findCountMessagesByUser(messageEntity);
			Map<String, Object> param = new HashMap<String, Object>();
			pageEntity.setTotalPage(count);
			pageEntity.setPageLess(pageEntity.getPage());
			param.put("pageEntity", pageEntity);
			param.put("messageEntity", messageEntity);
			List<MessageEntity> messages = messageService
					.findMessagesByUser(param);
			json.put("status", 200);
			json.put("messages", messages);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/insertUserEntityYouKe", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject insertUserEntityYouKe(
			@ModelAttribute UserEntity userEntity) {
		JSONObject json = new JSONObject();
		try {
			userService.insertUserEntityYouKe(userEntity);
			json.put("status", 200);
			json.put("userId", userEntity.getId());
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/updateUserLocation", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject updateUserLocation(@ModelAttribute UserEntity userEntity) {
		JSONObject json = new JSONObject();
		try {
			userService.updateUserEntityLocation(userEntity);
			json.put("status", 200);
			json.put("message", "修改成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}

	@RequestMapping(value = "/findContextCollectByUser", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject findContextCollectByUser(
			@ModelAttribute CcollectEntity ccollectEntity) {
		JSONObject json = new JSONObject();
		try {
			List<ContextEntity> contexts = ccollectService
					.findContextsCollectByUserId(ccollectEntity.getUser_id());
			json.put("status", 200);
			json.put("contexts", contexts);
		} catch (Exception ex) {
			ex.printStackTrace();
			json.put("status", 400);
			json.put("message", ex.getMessage());
		}
		return json;
	}
}
